const http = require('http');
const fs = require('fs');
const logFile='/opt/eleServer/info.log';
const errorFile='/opt/eleServer/error.log'
// const logFile='info.log';
// const errorFile='error.log'
main();

function main() {
  try {
    const server = http.createServer()
    server.on('request', function () {
      console.log('收到客户端的请求了')

    })
    // const server = http.createServer((req, res) => {
    //
    //   if (req.method === 'POST') {
    //     let body = '';
    //     req.on('data', chunk => {
    //       body += chunk.toString();
    //     });
    //     req.on('end', () => {
    //       console.log(body); // 这里就是POST请求的数据
    //       fs.appendFile(logFile, body + ';' + '\r\n', 'utf8', (err) => {
    //         if (err) throw err;
    //         console.log('数据已追加到文件');
    //       });
    //       res.end('hallow world');
    //     });
    //   } else {
    //     res.end('Send a POST request to receive data');
    //     fs.appendFile(errorFile, 'not post: ' + req.method + '\r\n', 'utf8', err => {
    //     });
    //   }
    //   const json = { url: req.url, method: req.method };
    //   fs.appendFile(logFile, JSON.stringify(json) + '\r\n', 'utf8', err => {
    //   });
    // });

    server.listen(9923,"0.0.0.0", () => {
      console.log('Server is running on http://localhost:9923');
      fs.appendFile(logFile, 'server is start' + '\r\n', 'utf8', err => {
      });
    });

  } catch (e) {
    fs.appendFile(errorFile, e + '\r\n', 'utf8', err => {
    });
  }
}


